package services

import (
	"go_ruoyi/global"
	"go_ruoyi/model/db_model"
	"go_ruoyi/model/request_model"
	"go_ruoyi/utils"
	"net/http"

	"github.com/gin-gonic/gin"
)

func RegisterService(c *gin.Context) {
	var register request_model.Register
	if err := c.ShouldBindJSON(&register); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 400,
			"msg":  "参数错误",
		})
		return
	}
	if !utils.VerifyCode(register.Code, register.Uuid) {
		c.JSON(http.StatusOK, gin.H{
			"code": 400,
			"msg":  "验证码错误",
		})
		return
	}
	if err := global.DB.Create(&db_model.User{
		UserName: register.Username,
		Password: register.Password,
	}).Error; err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 400,
			"msg":  "注册失败",
		})
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "注册成功",
	})
}
